from rest_framework import serializers
from .models import *

# 品牌的序列化器
class BrandSer(serializers.ModelSerializer):
    class Meta:
        logo = serializers.CharField(label='品牌logo', allow_null=True, trim_whitespace=True)
        model = Brand
        fields = "__all__"
        read_only_fields = [id,]

# 频道组的序列化器
class GroupSer(serializers.ModelSerializer):
    cate = serializers.PrimaryKeyRelatedField(queryset=Cate.objects.all())
    cate_name = serializers.SerializerMethodField(read_only=True)
    def get_cate_name(self,obj):
        return obj.cate.name

    class Meta:
        model = Group
        fields = "__all__"
        read_only_fields = [id,]

# 类别的序列化器
class CateSer(serializers.ModelSerializer):
    class Meta:
        model = Cate
        fields = '__all__'

# sku的序列化器
class GoodSer(serializers.ModelSerializer):
    cate = serializers.PrimaryKeyRelatedField(queryset=Cate.objects.filter(cid=None))
    spu = serializers.PrimaryKeyRelatedField(queryset=SPU.objects.all())
    cate_name = serializers.SerializerMethodField(read_only=True)

    def get_cate_name(self,obj):
        return obj.cate.name

    class Meta:
        model = Good
        fields = '__all__'
        read_only_fields = ['id','image_default',]

# SPU的序列化器
class SPUSer(serializers.ModelSerializer):
    brand = serializers.PrimaryKeyRelatedField(queryset=Brand.objects.all())
    brand_name = serializers.SerializerMethodField(read_only=True)
    def get_brand_name(self,obj):
        return obj.brand.name
    class Meta:
        model = SPU
        fields = '__all__'
        read_only_fields = ['id']

# spu商品规格的序列化器
class SPUSpecSer(serializers.ModelSerializer):
    # PrimaryKeyRelatedField相关字段
    spu = serializers.PrimaryKeyRelatedField(queryset=SPU.objects.all())
    spu_name = serializers.SerializerMethodField(read_only=True)

    def get_spu_name(self,obj):
        return obj.spu.name

    class Meta:
        model = SPUSpec
        fields = '__all__'
        read_only_fields = ['id']

# 规格选项值的序列化器
class SpecOptionSer(serializers.ModelSerializer):
    spec = serializers.PrimaryKeyRelatedField(queryset=SPUSpec.objects.all())
    spec_name = serializers.SerializerMethodField(read_only=True)

    def get_spec_name(self,obj):
        return obj.spec.name

    class Meta:
        model = SpecOption
        fields = '__all__'
        read_only_fields = [id]


# 商品图片的序列化器
class SKUImageSer(serializers.ModelSerializer):
    sku = serializers.PrimaryKeyRelatedField(queryset=Good.objects.all())
    sku_name = serializers.SerializerMethodField(read_only=True)

    def get_sku_name(self,obj):
        return obj.sku.name

    class Meta:
        model = SKUImage
        fields = '__all__'
        read_only_fields = [id]
